

#include <stdint.h>
#include "i7816_define.h"
#include "i7816_hal.h"
#include "i7816_log.h"

#include "i7816_hal_con_raw_define.h"
#include "i7816_hal_usart_define.h"

#include "gd32w51x.h"
#include "BSP_Uart_2.h"

i7816_con_raw_t i7816_con_raw = {
	.vcc = {(void*)GPIOB, GPIO_PIN_11},
	.reset = {(void*)GPIOA, GPIO_PIN_1},
	.io = AFIO_INIT(GPIOA, GPIO_PIN_0, GPIO_AF_0),
	.clock = AFIO_INIT(GPIOA, GPIO_PIN_2, GPIO_AF_2),	//选择TIM4_CH2功能
};

i7816_usart_t i7816_usart = {
	.id = 0,
	.transfer_direction = TRANSFER_OUT,
	.dev = USART0,
	.rcu = RCU_USART0,
	.iqrn = USART0_IRQn,
	.protocol = PROTOCOL_ANY,
	.FiDi = 0x00,
	.inverse = false,
};

i7816_hw_t i7816_hw = {
	.con = &i7816_con_raw,
	.usart = &i7816_usart,
	.id = 0,
};

i7816_ifd_t i7816_ifd[IFD_NUM] = {
	[0] = {.hw = &i7816_hw,}
};
/*======================================时钟信号===============================================*/

/*!
    \brief      configure the GPIO ports
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void gpio_config(void)
{
    rcu_periph_clock_enable(RCU_GPIOA);

    /*Configure PA0 PA1 PA2(TIMER1 CH0 CH1 CH2) as alternate function*/
    
    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_2);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_166MHZ,GPIO_PIN_2);
    gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_2);
}

#define I7816_CLK_TIMER			TIMER4
#define I7816_CLK_TIMER_RCU		RCU_TIMER4
#define I7816_CLK_TIMER_FREQ()	(rcu_clock_freq_get(CK_APB1) * 2)
#define I7816_CLK_TIMER_CH		TIMER_CH_2

/*!
    \brief      configure the TIMER peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void timer_config(uint32_t hz)
{
    /* -----------------------------------------------------------------------
    TIMER1 configuration: generate 3 PWM signals with 3 different duty cycles:
    TIMER1CLK = SystemCoreClock / 180 = 1MHz

    TIMER1 channel0 duty cycle = (4000/ 16000)* 100  = 25%
    TIMER1 channel1 duty cycle = (8000/ 16000)* 100  = 50%
    TIMER1 channel2 duty cycle = (12000/ 16000)* 100 = 75%
    ----------------------------------------------------------------------- */
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;
	
    uint32_t clk = I7816_CLK_TIMER_FREQ();
	uint32_t period = clk / hz;
	
    rcu_periph_clock_enable(I7816_CLK_TIMER_RCU);

    timer_deinit(I7816_CLK_TIMER);
	
    /* TIMER1 configuration */
    timer_initpara.prescaler         = 1-1;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = period-1;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(I7816_CLK_TIMER,&timer_initpara);

    /* CH0,CH1 and CH2 configuration in PWM mode */
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;

    timer_channel_output_config(I7816_CLK_TIMER,I7816_CLK_TIMER_CH,&timer_ocintpara);
	
    /* CH2 configuration in PWM mode0,duty cycle 50% */
    timer_channel_output_pulse_value_config(I7816_CLK_TIMER,I7816_CLK_TIMER_CH,period/2-1);
    timer_channel_output_mode_config(I7816_CLK_TIMER,I7816_CLK_TIMER_CH,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(I7816_CLK_TIMER,I7816_CLK_TIMER_CH,TIMER_OC_SHADOW_DISABLE);

    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(I7816_CLK_TIMER);
    /* auto-reload preload enable */
    timer_enable(I7816_CLK_TIMER);
//		while(1);
}

static void i7816_usart_clock_init(uint32_t freq)
{
//	    gpio_config();
    timer_config(freq);
}
/*======================================数据收发===============================================*/

static void i7816_usart_rx_cb(uint16_t data){
	if(i7816_usart.transfer_direction == TRANSFER_IN && i7816_usart.queue){
		BaseType_t xHigherPriorityTaskWoken = pdFALSE;
        xQueueSendFromISR(i7816_usart.queue, &data, &xHigherPriorityTaskWoken);
		portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
	}
}
extern uint32_t SystemCoreClock;
void i7816_config_init(void){
	uint32_t freq = SystemCoreClock / 32;
	BSP_Uart_SetRxCallback(i7816_usart.id, i7816_usart_rx_cb);
	i7816_hal_init(i7816_ifd[0].hw, freq);
	i7816_usart_clock_init(freq);
}

